﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Json;
using ToodledoAPI.DataTypes;
using ToodledoAPI.Exceptions;
using System.Linq;
namespace ToodledoAPI
{
    public static class contexts
    {
        public static void get(Key key, Action<Context[]> returnAction)
        {
            string Method = String.Format("contexts/get.php?key={0}", key);
            ApplicationConfiguration.CallWebService(Method, (s) =>
            {
                if (s.IsToodledoException())
                {
                    throw new contextException(s);
                }
                JsonArray Array = (JsonArray)JsonObject.Parse(s);
                returnAction(Array.Select((C) => new Context((JsonObject)C)).ToArray());

            });
        }

        public static void add(string name, Key key, Action<Context> returnAction)
        {
            string Method = String.Format("contexts/add.php?name={0};key={1}", name, key);
            ApplicationConfiguration.CallWebService(Method, (s) =>
            {
                if (s.IsToodledoException())
                {
                    throw new contextException(s);
                }
                JsonArray Array = (JsonArray)JsonObject.Parse(s);
                returnAction(new Context((JsonObject)Array[0]));
            });
        }

        public static void edit(int ID, string name, Key key, Action<Context> returnAction)
        {
            string Method = String.Format("contexts/edit.php?id={0};name={1};key={2}", ID, name, key);
            ApplicationConfiguration.CallWebService(Method, (s) =>
            {
                if (s.IsToodledoException())
                {
                    throw new contextException(s);
                }
                JsonArray Array = (JsonArray)JsonObject.Parse(s);
                returnAction(new Context((JsonObject)Array[0]));
            });
        }

        public static void delete(int id, Key key, Action<int> returnAction_Success)
        {
            string Method = String.Format("contexts/delete.php?id={0};key={1}", id, key);
            ApplicationConfiguration.CallWebService(Method, (s) =>
            {
                if (s.IsToodledoException())
                {
                    throw new contextException(s);
                }
                returnAction_Success(int.Parse(((JsonObject)JsonObject.Parse(s))["deleted"]));
            });
        }
    }
    namespace DataTypes
    {
        public class Context
        {
            public Context(JsonObject F)
            {
                id = int.Parse(F["id"]);
                name = F["name"];
            }

            public int id { get; set; }
            public string name { get; set; }
        }
    }
}
